package com.your.package.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.your.package.data.entity.User
import com.your.package.network.ApiService
import com.your.package.utils.EmailUtils
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class RegisterViewModel(
    private val apiService: ApiService,
    private val userDao: UserDao
) : ViewModel() {
    private val _registrationState = MutableStateFlow<RegistrationState>(RegistrationState.Idle)
    val registrationState: StateFlow<RegistrationState> = _registrationState

    fun register(email: String, password: String, name: String, verificationCode: String) {
        viewModelScope.launch {
            try {
                _registrationState.value = RegistrationState.Loading

                // 1. 验证邮箱格式
                if (!EmailUtils.isValidEmail(email)) {
                    _registrationState.value = RegistrationState.Error("无效的邮箱格式")
                    return@launch
                }

                // 2. 验证验证码
                val verifyResponse = apiService.verifyCode(email, verificationCode)
                if (!verifyResponse.isSuccessful || verifyResponse.body() != true) {
                    _registrationState.value = RegistrationState.Error("验证码错误")
                    return@launch
                }

                // 3. 转换邮箱为融云ID
                val rongCloudId = EmailUtils.convertEmailToRongCloudId(email)

                // 4. 注册融云用户
                val response = apiService.getRongCloudToken(rongCloudId, name)
                if (!response.isSuccessful || response.body()?.code != 200 || response.body()?.token == null) {
                    _registrationState.value = RegistrationState.Error("融云注册失败")
                    return@launch
                }

                // 5. 保存用户信息到本地数据库
                val user = User(
                    email = email,
                    password = password,
                    name = name,
                    rongCloudId = rongCloudId,
                    rongCloudToken = response.body()?.token
                )
                userDao.insertUser(user)

                _registrationState.value = RegistrationState.Success
            } catch (e: Exception) {
                _registrationState.value = RegistrationState.Error(e.message ?: "注册失败")
            }
        }
    }

    fun sendVerificationCode(email: String) {
        viewModelScope.launch {
            try {
                val response = apiService.sendVerificationCode(email)
                if (!response.isSuccessful) {
                    _registrationState.value = RegistrationState.Error("发送验证码失败")
                }
            } catch (e: Exception) {
                _registrationState.value = RegistrationState.Error("发送验证码失败")
            }
        }
    }
}

sealed class RegistrationState {
    object Idle : RegistrationState()
    object Loading : RegistrationState()
    object Success : RegistrationState()
    data class Error(val message: String) : RegistrationState()
} 